/**  
 * @Title: SystemContextInit.java
 * @Package: com.itic.appbase.framework.plugins.Spring
 * @author: idong
 * @date: 2015年5月8日 下午4:51:17
 * @version: V1.0  
 */

package com.itic.appbase.framework.plugins.sysinit;

import java.util.StringTokenizer;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.ServletContextAware;

import com.itic.appbase.framework.plugins.logger.Log;
import com.itic.appbase.framework.plugins.sysinit.configuration.ConfigurationInit;
import com.itic.appbase.framework.plugins.sysinit.webcontext.WebContextInit;
import com.itic.appbase.framework.utils.ReflectHelper;
import com.itic.appbase.framework.utils.SpringContextHelper;
import com.itic.appbase.framework.utils.StringHelper;

/**
 * <p>
 * Company: itic
 * </p>
 * 
 * @author: idong
 * @date: 2015年5月8日 下午4:51:17
 * @version: V1.0
 */
public class SystemContextInit
        implements InitializingBean, ServletContextAware {
    private String activeProfile;
    private String customInit;
    private ServletContext servletContext;
    private static final Logger LOGGER = Log.get(SystemContextInit.class);

    public SystemContextInit() {
        super();
    }

    /*
     * 初始化方法
     * 
     * @see
     * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
     */
    @Override
    public void afterPropertiesSet()
            throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("\r\n======================================================================\r\n");
        sb.append("\r\n    运行环境： " + this.getActiveProfile() + "\r\n");
        // 参数读取工具初始化
        ConfigurationInit.init(this.getActiveProfile());
        // 部署环境,版本初始化
        WebContextInit.init(this.getServletContext());
        // 数据初始化
        if (StringHelper.isNotBlank(this.getCustomInit())) {
            this.invokeCustomInit(this.getCustomInit());
        }
        sb.append("\r\n======================================================================\r\n");
        LOGGER.info(sb.toString());
    }

    /**
     * 执行自定义初始化方法
     * 
     * @param customInit
     */
    private void invokeCustomInit(String customInit) {
        StringTokenizer tokenizer = new StringTokenizer(customInit, ",");
        String token = "";
        String clazz = "";
        String method = "";
        Object obj = null;
        String[] callObject = new String[2];
        try {
            while (tokenizer.hasMoreTokens()) {
                token = tokenizer.nextToken();
                callObject = token.split("\\.");
                clazz = callObject[0];
                method = callObject[1];
                if (SpringContextHelper.getApplicationContext().containsBean(clazz)) {
                    obj = SpringContextHelper.getBean(clazz);
                    ReflectHelper.invokeMethod(obj, method, new Class[] {});
                }
            }
        } catch (Exception e) {
            LOGGER.error("Custom Init read Error", e);
        }
    }

    /**
     * 运行环境参数.
     * 
     * @return
     */
    public String getActiveProfile() {
        return activeProfile;
    }

    public void setActiveProfile(String activeProfile) {
        this.activeProfile = activeProfile;
    }

    /**
     * 自定义初始化方法
     * 
     * @return
     */
    public String getCustomInit() {
        return customInit;
    }

    public void setCustomInit(String customInit) {
        this.customInit = customInit;
    }

    /*
     * /* Context参数
     * 
     * @see
     * org.springframework.web.context.ServletContextAware#setServletContext
     * (javax.servlet.ServletContext)
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public ServletContext getServletContext() {
        return servletContext;
    }

}
